package com.wharf.base.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.wharf.base.model.Goods;
import com.wharf.base.model.GoodsCar;
import com.wharf.base.model.UserInfo;

import com.wharf.base.service.GoodsCarService;
import com.wharf.base.service.GoodsService;

@RestController
@RequestMapping("/backThree")
public class CartController extends BaseController{
	
		@Autowired
		private GoodsCarService goodscartservice;
		@Autowired
		private GoodsService goodService;
		
		//根据用户id查询商品id   2；根据商品id查询商品信息并且返回
		@ResponseBody
		@GetMapping("/goodsCar")
		public ResponseEntity<List<Goods>> showGoodsCart() {
			//得到登录的用户信息
			UserInfo user1 = (UserInfo) session.getAttribute(LOGIN_USER);
			System.out.println(user1);
			System.out.println(user1.getUserId());
			int i= user1.getUserId();
			System.out.println(user1.getInfoName());
			System.out.println(1);
			//根据用户id查询得到商品id
			List<GoodsCar> GoodsCar = goodscartservice.selectGoodsIdByUserId(i);
			System.out.println(GoodsCar);
			//查询商品信息并且返回给前端
			List<Goods> goods = new ArrayList<Goods>();
			GoodsCar.forEach(goodsCar->{
				List<Goods> goodlist = goodService.selectById(goodsCar.getGoodsId());
				goods.add(goodlist.get(0));
				
			});
			//ResponseEntity返回响应
			System.out.println(goods);
			return new ResponseEntity<List<Goods>>(goods, HttpStatus.OK);
		}
		
		@GetMapping("/addgoodsid")
		public int insertGoodsId(Integer id) {
			int i = goodscartservice.addGoodsIdInCar(id);
			return i;
		}
}
